$(function () {
    var ua = navigator.userAgent.indexOf('Android');
    if (ua > -1) {
        $('.ipt').on('focus', function () {
            $('.css').css({ 'visibility': 'hidden' })
        }).on('blur', function () {
            $('.css').css({ 'visibility': 'visible' })
        })
    }
    // var webUrl = 'https://api.wdb007.com/wdb007/';
    var webUrl = ' https://api.wdb007.com/wdb-wxapp-api-test/';

    var hostUrl = 'https://api.wdb007.com/wdb-wxapp-test/';
    // var hostUrl = 'https://api.wdb007.com/wdb-wxapp/';

    var Request = new Object();
    Request = GetRequest();
    var qrStr = Request['qr'];
    var coupon_rule_id = Request['ruleid']
    var usertoken = Request['usertoken']
    var u = Request['u']

    function getCouponInfo() {
        if (coupon_rule_id) {
            var params = {
                coupon_rule_id: coupon_rule_id
            };
            var url = webUrl + 'coupon/getCouponRuleInfo';
            $.ajax({
                type: 'GET',
                data: params,
                url: url,
                success: function (res) {
                    console.log(res);
                    var coupon_cash = res.detail.coupon_cash;
                    var coupon_condition = res.detail.coupon_condition;
                    var coupon_desc = res.detail.coupon_desc;
                    var coupon_type = res.detail.coupon_type;
                    var over_time = res.detail.over_time;
                    sessionStorage.coupon_desc = coupon_desc;
                    $('.coupon_cash').html(coupon_cash);
                    $('.coupon_info_02').html(coupon_condition);
                    $('.coupon_type').html(coupon_type);
                    $('.coupon_detail').html(coupon_desc);
                    $('.time_detail').html(over_time);
                },
                error: function (err) {
                    console.log('err', err);
                }
            });
        }
    }
    getCouponInfo()

    $("#send_code_btn").click(function () {
        getCode();
    });

    // // 获取验证码  
    function getCode() {
        var userPhone = $("#user_phone").val();
        if (isMobile(userPhone)) {
            var url = webUrl + 'user/getRegisterCode?mobile_no=' + userPhone;
            $.when(get(url)).done(function (data) {
                if (data.code == 1010070009) {
                    resetCode(); //倒计时
                } else {
                    $('.err_info').html('验证码发送失败');

                }
            }).catch(function (err) {
                $('.err_info').html(JSON.stringify(err));
            });
        } else {
            $('.err_info').html('请填写正确的手机号码');
        }
    }

    // //倒计时
    function resetCode() {
        $('#send_code_btn').hide();
        $('#J_second').html('60');
        $('#J_resetCode').show();
        var second = 60;
        var timer = null;
        timer = setInterval(function () {
            second -= 1;
            if (second > 0) {
                $('#J_second').html(second);
            } else {
                clearInterval(timer);
                $('#send_code_btn').show();
                $('#J_resetCode').hide();
            }
        }, 1000);
    }

    //    点击领取月卡
    $("#check_btn").click(function () {
        var code = $("#user_code").val();
        var userPhone = $("#user_phone").val();
        console.log(code, userPhone);
        if (registerCode(code)) {
            var postData = {
                mobile: userPhone,
                verify_code: code,
                scanCode: qrStr
            };
            var url = hostUrl + 'scan/scanActivityByPhone';
            $.ajax({
                type: 'POST',
                data: postData,
                url: url,
                success: function (res) {
                    console.log(res);
                    if (res.code == 0) {
                        location.href = "html/scanActiSucc.html?code=0";
                    } else if (res.code == 10001) {
                        location.href = "html/scanActiSucc.html?code=1";
                    } else if (res.code == 1010070017) {
                        location.href = "html/scanActiSucc.html?code=2";
                    } else if (res.code == 10002) {
                        $('.err_info').html(res.message);
                    } else {
                        $('.err_info').html(res.message);
                    }

                },
                error: function (err) {
                    console.log('err', err);
                    $('.err_info').html(err);
                }
            });
        } else {
            $('.err_info').html('请输入正确的验证码');
        }

    });
    // 点击领取优惠券
    $('#get_btn').click(function () {
        var code = $("#user_code").val();
        var userPhone = $("#user_phone").val();
        console.log(code, userPhone);
        if (registerCode(code)) {
            var postData = {
                mobile_no: userPhone,
                verify_code: code,
                coupon_rule_id: coupon_rule_id,
                usertoken: usertoken,
                u: u
            };
            var url = webUrl + 'coupon/drawCoupon';
            $.ajax({
                type: 'POST',
                data: postData,
                url: url,
                success: function (res) {
                    console.log(res);

                    if (res.code == 0) {
                        wx.miniProgram.redirectTo({
                            url: `../../success/scan_success/scan_success?coupon_desc=${res.detail.coupon_desc}&successType=1`
                        })
                    } else if (res.code == 1010070020) {
                        wx.miniProgram.redirectTo({
                            url: `../../success/scan_success/scan_success?erromsg=${res.message}&successType=0`
                        })
                    } else {
                        $('.err_info').html(res.message);
                    }
                },
                error: function (err) {
                    $('.err_info').html('领取失败');
                }
            });
        } else {
            $('.err_info').html('请输入正确的验证码');
        }

    });
    // 九宫格抽奖登陆
    $('#lottery_btn').click(function () {
        var code = $("#user_code").val();
        var userPhone = $("#user_phone").val();
        console.log(code, userPhone);
        if (registerCode(code)) {
            var postData = {
                mobile_no: userPhone,
                verify_code: code,
                usertoken: sessionStorage.getItem('usertoken')
            };

            var url = webUrl + 'user/login';
            var header = getHeader(url, postData);
            $.ajax({
                type: 'POST',
                data: postData,
                url: url,
                headers: header,
                success: function (res) {
                    console.log(res);
                    if (res.result == 1) {
                        sessionStorage.setItem('usertoken', res.detail.usertoken);
                        sessionStorage.setItem('userid', res.detail.userid);

                        $('#lottery_page').show();
                        $('#lottery_login').hide();
                        wx.miniProgram.navigateTo({
                            url: '../../success/lottery_success/lottery_success?gift_content=' + sessionStorage.getItem('lotterySuccessContent') + '&usertoken=' + res.detail.usertoken + '&userid=' + res.detail.userid
                        })

                    } else {
                        $('.err_info').html(res.message);
                    }

                },
                error: function (err) {
                    $('.err_info').html('登陆失败');

                }
            });
        } else {
            $('.err_info').html('请输入正确的验证码');
        }
    })
});